package com.gao.thread;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
public class ThreadTest {
    /*
        需求：某电影院目前上映国产大片，共有一百张票，而它有3个窗口卖票，请设计一个程序模拟该电影院卖票
        -多条线程共享一份资源
     */
    public static void main(String[] args) {
        TicketTask task = new TicketTask();
        Thread t1 = new Thread(task,"窗口1");
        Thread t2 = new Thread(task,"窗口2");
        Thread t3 = new Thread(task,"窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}

class TicketTask implements Runnable {
    private int tickets = 100;
private Object o = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (o){
                if (tickets <= 0) {
                    break;
                }
                System.out.println(Thread.currentThread().getName() +"卖了"+ tickets+"张票");
                tickets--;
            }

        }
    }
}